/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2021年6月7日
 */
package com.massyframework.beanskin.util.exceptioncollect;

/**
 * 例外收集组工具类
 * @author huangkh
 *
 */
public abstract class ExceptionCollectUtils {

	private static ThreadLocal<ExceptionGroup> LOCAL =
			new ThreadLocal<ExceptionGroup>();
	
	/**
	 * 添加例外
	 * @param exception {@link Exception}
	 */
	public static void addException(Exception exception) {
		ExceptionGroup group = LOCAL.get();
		if (group != null) {
			group.addException(exception);
		}
	}
	
	/**
	 * 当前线程的例外收集组
	 * @return {@link ExceptionGroup}
	 */
	public static ExceptionGroup getCurrent() {
		return LOCAL.get();
	}
	
	/**
	 * 向当前线程绑定例外收集组
	 * @param ecg
	 */
	public static void bind(ExceptionGroup group) {
		if (group != null) {
			LOCAL.set(group);
		}
	}
	
	/**
	 * 取消当前线程绑定的例外收集组
	 * @return {@link ExceptionGroup},例外收集组
	 */
	public static ExceptionGroup unbind() {
		ExceptionGroup result = LOCAL.get();
		LOCAL.set(null);
		return result;
	}
}
